﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Diagnostics;


namespace GPC_Overlay
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]


        static void Main()
        {
            string Time;
            GpcOperation Timed;
            Timed = (GpcOperation)Enum.Parse(typeof(GpcOperation), "Intersection");
            Stopwatch sw = new Stopwatch();
            sw.Start();
            //Parser overlayParser = new Parser("C:\\Users\\groupUser\\Desktop\\GIS Data\\parcelXMLLarge.xml");
            //Parser baseParser = new Parser("C:\\Users\\groupUser\\Desktop\\GIS Data\\polygon1XML.xml");

            Parser overlayParser = new Parser("C:\\Users\\dinesh\\Desktop\\gpcoverlay\\gpcfiles\\parceLXML.xml");
            Parser baseParser = new Parser("C:\\Users\\dinesh\\Desktop\\gpcoverlay\\gpcfiles\\polygon1XML.xml");
           
            List<Polygon> basePoly = new List<Polygon>();
            basePoly = baseParser.poly;
            List<Polygon> overLayPoly = new List<Polygon>();
            overLayPoly = overlayParser.poly;
            List<Polygon> result = new List<Polygon>();
            sw.Stop();
            Time = "Time for Parsing = " + sw.Elapsed.ToString() + " ";
            sw.Restart();
           
            //ParallelSort On lowerBox (send true as parameter)
            ParallelSort.QuicksortParallel(overLayPoly, true);
            //ParallelSort.QuicksortSequential(overLayPoly, true);

            Parser.createRelationships(ref basePoly,ref overLayPoly);

            GpcOperation operation = GpcOperation.Intersection;

            int num = 0;
            for (int i = 0; i < basePoly.Count; i++)
            {
                int count = basePoly[i].overlayPolygons.Count;
                for (int r = 0; r < count; r++)
                {
                    Polygon polygon = new Polygon();
                    polygon = basePoly[i].Clip(operation, basePoly[i].overlayPolygons[r]);
                    polygon.fID = basePoly[i].fID + "-" + basePoly[i].overlayPolygons[r].fID + "--" + num.ToString();
                    polygon.srsname = basePoly[i].srsname;
                    polygon.srsDimension = basePoly[i].srsDimension;
                    result.Add(polygon);
                    num++;
                }
            }
            sw.Stop();
            Time += "Time for processing = " + sw.Elapsed.ToString() + " ";
            sw.Restart();
            sw.Start();
            Parser.Save(result, "C:\\Users\\dinesh\\Desktop\\gpcoverlay\\gpcfiles\\polygonout.xml");
            sw.Stop();
            Time += "Time for Saving = " + sw.Elapsed.ToString();
            Console.WriteLine(Time);
            Console.ReadKey();
        }

    }
}
